package com.jxb.bojiaoMybatis;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import com.sun.istack.internal.NotNull;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/11/23 20:25
 **/
public class ZhouyuImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
                                        BeanNameGenerator importBeanNameGenerator) {
        //扫描路径
        Map<String, Object> annotationAttributes =
                importingClassMetadata.getAnnotationAttributes(BojiaoMybatisScan.class.getName());
        String path = (String) annotationAttributes.get("value");

        //通过扫描器进行扫描
        BeanDefinitionScanner beanDefinitionScanner = new BeanDefinitionScanner(registry);
        beanDefinitionScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(@NotNull MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
                    throws IOException {
                return true;
            }
        });
        beanDefinitionScanner.doScan(path);
    }
}
